#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>

using namespace std;




  struct ListNode
  {
      int val;
      struct ListNode *next;
  };
 


struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
    int Stack1[100] = { 0 }, Stack2[100] = { 0 };
    int Top1 = -1, Top2 = -1;
    while (l1 != NULL)
    {
        Stack1[++Top1] = l1->val;
        l1 = l1->next;
    }
    while (l2 != NULL)
    {
        Stack2[++Top2] = l2->val;
        l2 = l2->next;
    }
    int Main = Top1 > Top2 ? -1 : 1;
    int Answer[101], TopA = -1;
    int i = 0, j = 0;
    while (i <= Top1 && j <= Top2)
    {
        int sum = Stack1[i++] + Stack2[j++];
        if (sum < 10)
            Answer[++TopA] = sum;
        else
        {
            if (Main == -1)
            {
                Answer[++TopA] = sum % 10;
                Stack1[i] += sum / 10;
                if (i > Top1)Top1 = i;
            }
            else if (Main == 1)
            {
                Answer[++TopA] = sum % 10;
                Stack2[j] += sum / 10;
                if (j > Top2)Top2 = j;
            }
        }
    }
    while (i <= Top1 && j > Top2)
    {
        if (Stack1[i] < 10)
            Answer[++TopA] = Stack1[i++];
        else
        {
            Answer[++TopA] = Stack1[i] % 10;
            Stack1[i + 1] += Stack1[i] / 10;
            if (i + 1 > Top1)Top1 = i + 1;
            i++;
        }
    }
    while (j <= Top2 && i > Top1)
    {
        if (Stack2[j] < 10)
            Answer[++TopA] = Stack2[j++];
        else
        {
            Answer[++TopA] = Stack2[j] % 10;
            Stack2[j + 1] += Stack2[j] / 10;
            if (j + 1 > Top2)Top2 = j + 1;
            j++;
        }
    }
    int flag = 0;
    struct ListNode* List = NULL;
    struct ListNode* Last = NULL;
    for (int i = 0; i <= TopA; i++)
    {
        if (!flag)
        {
            List = (struct ListNode*)malloc(sizeof(struct ListNode));
            List->next = NULL;
            List->val = Answer[i];
            Last = List;
            flag++;
        }
        else
        {
            Last->next = (struct ListNode*)malloc(sizeof(struct ListNode));
            Last = Last->next; Last->next = NULL;
            Last->val = Answer[i];
        }
    }
    return List;
}


int main()
{
    ListNode* n1 = (ListNode*)malloc(sizeof(ListNode));
    ListNode* n2 = (ListNode*)malloc(sizeof(ListNode));
    ListNode* n3 = (ListNode*)malloc(sizeof(ListNode));
    ListNode* n4 = (ListNode*)malloc(sizeof(ListNode));
    ListNode* n5 = (ListNode*)malloc(sizeof(ListNode));
    ListNode* n6 = (ListNode*)malloc(sizeof(ListNode));

    n1->next = n2;
    n2->next = n3;
    n3->next = NULL;
    n1->val = 2;
    n2->val = 4;
    n3->val = 3;
    n4->next = n5;
    n5->next = n6;
    n6->next = NULL;
    n4->val = 5;
    n5->val = 6;
    n6->val = 4;

    ListNode* n7 = addTwoNumbers(n1, n4);
    ListNode* cur = n7;
    while (cur)
    {
        printf("%d ", cur->val);
        cur = cur->next;
    }
    printf("\n");

    return 0;
}